use Component;

package LogManager;

@ISA=qw/Component/;

sub new{
    my $class = shift;
    my $self = {};

    bless $self, $class;
    $self->init(@_);

    return $self;
} # new

sub init{
    my $self = shift;

    $self->dependencies(qw/LOGPATH LOGENABLE/);
} # init

sub start{
    my $self = shift;
    my $fhandle;

    open $fhandle, ">>".$self->{LOGPATH};

    # Making the handle "Hot" to prevent buffering
    # Snippet from:
    # http://perl.plover.com/FAQs/Buffering.html
    { 
	my $old_fhandle = select $fhandle;
	$| = 1;
	select $old_fhandle;
    }

#    print "Opening ".$self->{LOGPATH}."\n";
#    print "Result = ".$fhandle."\n";
    $self->{LOGFILE} = $fhandle;
 #   print "Stored = ".$self->{LOGFILE}."\n";
}

sub end{
    my $self = shift;
    my $fhandle = $self->{LOGFILE};

    close $fhandle;
    $self->{LOGFILE} = undef;
}

sub log{
    my $self = shift;
    my $msg = shift;
    my $fhandle = $self->{LOGFILE};
    
    return unless(defined $fhandle);
    if(defined ($self->{LOGENABLE}) and $self->{LOGENABLE} == 1){
	print $fhandle $msg."\n";
    }
}
